/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage.Controls
{
	import flash.display.DisplayObject;
	
	public class BorderLayout extends Control
	{
		public function BorderLayout()
		{
			super();
		
			NoScale = true;
			
			RequireLayout();
		}
		
	
		private var itemTop:DisplayObject;
		private var itemBottom:DisplayObject;
		private var itemLeft:DisplayObject;
		private var itemRight:DisplayObject;
		private var itemCenter:DisplayObject;
		
		public function get ItemTop():DisplayObject { return itemTop; }
		public function get ItemBottom():DisplayObject { return itemBottom; }
		public function get ItemLeft():DisplayObject { return itemLeft; }
		public function get ItemRight():DisplayObject { return itemRight; }
		public function get ItemCenter():DisplayObject { return itemCenter; }
		
		private function SetItem(currentItemName:String, newItem:DisplayObject):void
		{
			var currentItem:DisplayObject = this[currentItemName];
			if( currentItem != null ) removeChild(currentItem); 
			
			if( newItem != null ) addChild(newItem);
			this[currentItemName] = newItem;
			
			RequireLayout();
		}
		
		private var spacing:Number = 0;
		public function set Spacing(value:Number):void { spacing = value; RequireLayout(); }
		
		public function set ItemTop(value:DisplayObject):void { SetItem("itemTop", value); }
		public function set ItemBottom(value:DisplayObject):void { SetItem("itemBottom", value); }
		public function set ItemLeft(value:DisplayObject):void { SetItem("itemLeft", value); }
		public function set ItemRight(value:DisplayObject):void { SetItem("itemRight", value); }
		public function set ItemCenter(value:DisplayObject):void { SetItem("itemCenter", value); }
		
		protected override function Layout():void
		{
			var t:Number = 0;
			if( itemTop != null ) 
			{
				t = itemTop.height;
				t += spacing;
				itemTop.x = 0;
				itemTop.y = 0;
				
			}
			
			var b:Number = 0;
			if( itemBottom != null )
			{
				b = itemBottom.height;
				b += spacing;
				itemBottom.x = 0;
				itemBottom.y = height - itemBottom.height;
			}
			
			if( itemCenter != null )
			{
				itemCenter.width = width;
				itemCenter.height = height - (t + b);
				itemCenter.x = 0;
				itemCenter.y = t;
			}
		}
		
	}
}













